package br.com.tartarugasindomaveis.ti.app;

import java.util.List;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.Gallery.LayoutParams;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;

public class AlbumFoto extends Activity implements OnItemSelectedListener,
		ViewFactory {

	private Drawable[] mImageIds;
	private Drawable[] mThumbIds;
	private ProgressDialog dialog;
	private Gallery g;
	ThreadProgress td;
	private String url;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);

		setContentView(R.layout.album_foto);

		// Captura o parametro passado ao detalhe
		Bundle b = getIntent().getExtras();
		url = b.getString("url");

		dialog = ProgressDialog
				.show(AlbumFoto.this, "Aguarde", "Carregando...");
		dialog.setCancelable(true);
		dialog.setCanceledOnTouchOutside(false);

		mSwitcher = (ImageSwitcher) findViewById(R.id.imgswitcher);
		mSwitcher.setFactory(this);
		mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
				android.R.anim.fade_in));
		mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
				android.R.anim.fade_out));

		td = new ThreadProgress();
		td.start();
	}

	Handler handler = new Handler(new Callback() {

		public boolean handleMessage(final Message msg) {
			runOnUiThread(new Runnable() {
				public void run() {
					g = (Gallery) findViewById(R.id.gallery);
					g.setAdapter(new ImageAdapter(AlbumFoto.this));
					g.setOnItemSelectedListener(AlbumFoto.this);
					dialog.dismiss();
				}
			});
			return false;
		}
	});

	public class ThreadProgress extends Thread implements Runnable {

		@Override
		public void run() {
			List<Album> album;
			PicasaService picasaService = new PicasaService();
			album = picasaService.carregaImagens(url);

			mImageIds = new Drawable[album.size()];
			mThumbIds = new Drawable[album.size()];
			CarregaImagem picasaImagem = new CarregaImagem();
			int it = 0;

			while (it < album.size()) {
				try {
					mImageIds[it] = picasaImagem.getImagem(((Album) album
							.get(it)).getFoto());

					mThumbIds[it] = picasaImagem.getImagem(((Album) album
							.get(it)).getMiniatura());

					it++;
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

			Message message = new Message();
			message.arg1 = 1;
			handler.sendMessage(message);
		}

	}

	public void onItemSelected(AdapterView<?> parent, View v, int position,
			long id) {
		mSwitcher.setImageDrawable(mImageIds[position]);
	}

	public void onNothingSelected(AdapterView<?> parent) {
	}

	public View makeView() {
		ImageView i = new ImageView(this);
		i.setBackgroundColor(0xFF000000);
		i.setScaleType(ImageView.ScaleType.FIT_CENTER);
		i.setLayoutParams(new ImageSwitcher.LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
		return i;
	}

	private ImageSwitcher mSwitcher;

	public class ImageAdapter extends BaseAdapter {
		public ImageAdapter(Context c) {
			mContext = c;
		}

		public int getCount() {
			return mThumbIds.length;
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			ImageView i = new ImageView(mContext);

			i.setImageDrawable(mThumbIds[position]);
			i.setAdjustViewBounds(true);
			i.setLayoutParams(new Gallery.LayoutParams(
					LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
			i.setBackgroundResource(R.drawable.picture_frame);
			return i;
		}

		private Context mContext;
	}
}
